package com.games.game.controller;

import com.games.game.bean.Game;
import com.games.game.bean.UserInfo;
import com.games.game.service.Game_Service;
import com.games.game.service.User_Game_Service;
import com.games.game.service.User_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private User_Service user_Service;
    @Autowired
    private User_Game_Service user_game_service;
    @Autowired
    private Game_Service game_service;

    @RequestMapping(value = "/{username}/profiles/modify_password", method = RequestMethod.PUT)
    public boolean modify_password(@PathVariable(value="username") String username, String u_password, String n_password) {
        UserInfo userInfo = user_Service.logIn(username, u_password);
        if (userInfo == null)
            return false;
        user_Service.modify_password(username, n_password);
        return true;
    }

    @RequestMapping(value = "/{username}/profiles/inventory", method = RequestMethod.GET)
    public List<Game> all_game(@PathVariable(value="username") String username) {
        List<String> list_str = user_game_service.get_all_game(username);
        List<Game> list = new ArrayList<>();
        for (String str: list_str) {
            list.add(game_service.get_cer_games(str));
        }
        return list;
    }
    // 得到用户所有游戏

    @RequestMapping(value = "/{username}/profiles/photo", method = RequestMethod.POST)
    public void update_photo(@PathVariable(value="username") String username, String photo) {
        user_Service.update_photo(username, photo);
    }
    // 上传头像

    @RequestMapping(value = "/{username}/{gamename}", method = RequestMethod.GET)
    public boolean has_game(@PathVariable(value = "username") String username, @PathVariable(value = "gamename") String gamename) {
        return user_game_service.has_game(username, gamename);
    }
    // 用户是否有该游戏

    @RequestMapping(value = "/{username}/{gamename}", method = RequestMethod.POST)
    public void purchase_game(@PathVariable(value = "username") String username, @PathVariable(value = "gamename") String gamename) {
        game_service.purchase(gamename);
        user_game_service.purchase_game(username, gamename);
    }
    // 用户购买游戏

    @RequestMapping(value = "/{username}/{gamename}", method = RequestMethod.DELETE)
    public void refund(@PathVariable(value = "username") String username, @PathVariable(value = "gamename") String gamename) {
        game_service.refund(gamename);
        user_game_service.refund(username, gamename);
    }
    // 用户退款

}
